$(function() {
	layui.use(['form', 'layer'], function() {
		var form = layui.form;
		form.render();
		var layer = layui.layer;
        BaseUtil.ajaxSetup();
        //接收从父页面传过来的roleId,目的是根据id查询，数据回显
       var roleId=BaseUtil.GetQueryString("roleId");
      //定义一个全局变量为我所拥有的权限数组
      var listRolePermission=[];
       function find_role_info(roleId){
       	$.ajax({
       		type:"get",
       		url:"http://localhost:8080/ssmService/role/selectRoleVoById/"+roleId,
       		async:false,
       		"success":function(result){
       			if(result.resultCode==0){
       				build_role_info(result,roleId);	
       				listRolePermission=result.data.listRolePermission;
       				version = result.data.role.version;
       			}
       		
       		}
       	});
       }
       //角色信息详情的数据渲染
       function build_role_info(result,roleId){
       	 var role=result.data.role;
       	$("input[name='role.name']").val(role.name);
        $("textarea[name='role.memo']").val(role.memo);
        form.render();
       }
       
       find_role_info(roleId);
		//定义获取权限列表的函数
		function find_menu_list() {
			$.ajax({
				type: "get",
				url: "http://localhost:8080/ssmService/index/selectEnumRoleMenu",
				async: true,
				"success": function(result) {
					console.log(result);
					build_menu_list(result,listRolePermission);
					
				}
			});
		}


		//定义渲染权限列表函数
		function build_menu_list(result,listRolePermission) {
			var menuList = result.data;
			$.each(menuList, function(index, item) {
				var parentDiv = $("<div></div>").addClass("layui-form-item");
				var smDiv = $("<div></div>").addClass("layui-input-block");
				 var flag=0;
				 var permission_value="";//定义变量用来接收子集删除修改等下标，但是前条件的具备父亲权限，否则这个值为空
				 for(var i=0;i<listRolePermission.length;i++){
				 	if(item.code==listRolePermission[i].code){
				 		flag=1;
				 		permission_value=listRolePermission[i].permission_value;
				 	}	
				 }
				 var parentCheckBox;
				 if(flag==0){
				 	parentCheckBox=$("<input type='checkbox' class='parentCheckBox' lay-filter='parentCheckBox'  value='" + item.code + "' title='" + item.name + "' >");
				 }else{
				    parentCheckBox=$("<input type='checkbox' class='parentCheckBox' lay-filter='parentCheckBox'  value='" + item.code + "' title='" + item.name + "' checked >");
				 }
				
				smDiv.append(parentCheckBox);
				var smCheckBoxAttr = item.roleValue.split(",");
			    var  permission_valueAttr;//切割好的字符串的数组，如果permission_value为空的话则不切割
				if(permission_value!=""){
					permission_valueAttr=permission_value.split('');
				}
			   
				for(var i = 0; i < smCheckBoxAttr.length; i++) {
					var smCheckBox
					if(permission_value!=""){//判断他的值是否为空，若为空则表明没有子集则表明无任何权限，直接所有的checkBox不选中
					if(permission_valueAttr[i]==0){//若permission_value不为空，则permission_valueAttr具备数组，那么就判断下标的值为0还是为1
						smCheckBox = $("<input type='checkbox'  class='childrenCheckBox'  lay-filter='childrenCheckBox' title='" + smCheckBoxAttr[i] + "' >");
						
					}else{
				    	smCheckBox = $("<input type='checkbox'  class='childrenCheckBox'  lay-filter='childrenCheckBox' title='" + smCheckBoxAttr[i] + "'  checked>");
					}
						
					}else{
						smCheckBox = $("<input type='checkbox'  class='childrenCheckBox'  lay-filter='childrenCheckBox' title='" + smCheckBoxAttr[i] + "' >");
					}
					
					
					smDiv.append(smCheckBox);
				}
				parentDiv.append(smDiv);
				$(".menu_list").append(parentDiv);
			});
			form.render();

		}
		//表单绑定事件,全选效果
		form.on("checkbox(parentCheckBox)", function(data) {
			if(data.elem.checked) {
				$(this).nextAll("input[type='checkbox']").prop("checked", true);
			} else {
				$(this).nextAll("input[type='checkbox']").prop("checked", false);
			}
			form.render();

		});
		
		form.on("checkbox(childrenCheckBox)", function(data) {
			var checkLength = 0;
			$(this).parent().children("input[class='childrenCheckBox']").each(function() {
				if($(this).is(":checked")) {
					checkLength++
				}
			});
			console.log(checkLength);
			if(checkLength > 0) {
				$(this).parent().children("input[class='parentCheckBox']").prop("checked", true);
			} else {
				$(this).parent().children("input[class='parentCheckBox']").prop("checked", false);
			}
			form.render();

		});

		$(".updateRole").click(function() {
        	add_menu_name();
          var token=BaseUtil.uuid(16,16);
       		 $("#token").val(token);
       		var role_id = parseInt(roleId);
			$("#roleId").val(roleId);
			$("#version").val(version);
		});
     
        form.on("submit(updateRole)",function(data){
        	var params=data.field;
           update_Role(params);
           return false;
        	
        });
        function update_Role(params){
        	$.ajax({
        		type:"post",
        		url:"http://localhost:8080/ssmService/role/updateRoleVo",
        		async:true,
        		data:params,
        		"success":function(result){
        			if(result.resultCode==0){
        				layer.msg("修改成功！",{
        					icon:1,
        					time:1000
        					
        				});
        			}
        			parent.location.reload();
        		}
        	});
        }
  
		function add_menu_name() {
			var index = 0;
			$(".parentCheckBox").each(function() {
				if($(this).is(":checked")) {
					$(this).attr("name", "listRolePermission[" + index + "].code");
					var value = "";
					$(this).nextAll("input[class='childrenCheckBox']").each(function() {

						if($(this).is(":checked")) {
							value += "1";
						} else {
							value += "0";
						}

					});
					var hidden = $("<input type='hidden' name='listRolePermission[" + index + "].permission_value' value='" + value + "'>");
					$(this).before(hidden);
					index++;
				}
			});

		}

		find_menu_list();
	});
})